<template>
  <BasicModal width="800px" @register="register" >
    <BasicTable @register="registerTable" :searchInfo="searchInfo" >
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增配置</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              tooltip: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, reactive } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getdictdataByPage, dictdataapi } from '../../system-api';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useModal } from '/@/components/Modal';
  import AccountModal from './dictdataModal.vue';

  import { columns, searchFormSchema } from './dictdata.data';
  import { useGo } from '/@/hooks/web/usePage';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'AccountManagement',
    components: { BasicTable, BasicModal, AccountModal, TableAction },
    setup() {
      const { createMessage } = useMessage();
      const { info } = createMessage;
      const go = useGo();
      const [registerModal, { openModal }] = useModal();
      const searchInfo = reactive<Recordable>({});
      let typeId;
      const [registerTable, { reload, updateTableDataRecord }] = useTable({
        title: '列表内容',
        api: getdictdataByPage,
        beforeFetch(params) {
          params.typeId = typeId;
          return params;
        },
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        fetchSetting: {
          pageField: 'page',
          sizeField: 'pageSize',
          listField: 'rows',
          totalField: 'totalRows',
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        handleSearchInfoFn(info) {
          return info;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });
     const [register] =  useModalInner((data) => {
        typeId = data.record.id;
        reload();
        console.log(data.record);
      });
      function handleCreate() {
        openModal(true, {
          record: { typeId: typeId },
          isUpdate: false,
        });
        //  console.log(openModal);
      }

      function handledict(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }
      function handleEdit(record: Recordable) {
        //console.log(record);
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        const ret = await dictdataapi.sysDictDataDeletePost(record);
        info(ret.data.message);
        reload();
        //console.log(ret);
      }

      function handleSuccess({ isUpdate, values }) {
        if (isUpdate) {
          // 演示不刷新表格直接更新内部数据。
          // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
          const result = updateTableDataRecord(values.id, values);
          //console.log(result);
        } else {
          reload();
        }
      }

      function handleSelect(deptId = '') {
        searchInfo.deptId = deptId;
        console.log(deptId);
        reload();
      }

      function handleView(record: Recordable) {
        go('/system/account_detail/' + record.id);
      }

      return {
        register,
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleSelect,
        handleView,
        searchInfo,
        handledict,
      };
    },
  });
</script>

